package com.gis3c.sys.dao.impl;

import com.gis3c.sys.dao.DictionaryDao;
import com.gis3c.sys.entry.po.Dictionary;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hukekuan on 2018/2/6.
 */
@Repository
public class DictionaryDaoImpl extends SqlSessionDaoSupport implements DictionaryDao {
    @Resource(name="sqlSessionFactory_sys") private SqlSessionFactory sqlSessionFactory;

    @PostConstruct
    public void injectSessionFactory(){
        super.setSqlSessionFactory(sqlSessionFactory);
    }

    @Override
    public List<Dictionary> findDictionarysByType(String dictType) {
        Map<String,Object> params = new HashMap<>();
        params.put("dictType",dictType);

        return getSqlSession().selectList("DictSql.findDictionarysByType",params);
    }

    @Override
    public List<Dictionary> findDictionarysByPage(String dictType, int pageSize, int currentPage) {
        Map<String,Object> params = new HashMap<>();
        params.put("dictType",dictType);
        params.put("pageSize",pageSize);
        params.put("currentPage",currentPage);
        return getSqlSession().selectList("DictSql.findDictionarysByPage",params);
    }

    @Override
    public Dictionary findDictionaryById(String dictId) {
        Map<String,Object> params = new HashMap<>();
        params.put("dictId",dictId);

        return getSqlSession().selectOne("DictSql.findDictionaryById",params);
    }

    @Override
    public List<Dictionary> findDictionarysByParent(String parentId) {
        Map<String,Object> params = new HashMap<>();
        params.put("parentId",parentId);

        return getSqlSession().selectList("DictSql.findDictionarysByParent",params);
    }

    @Override
    public Integer insertDictionary(Dictionary dictionary) {
        return getSqlSession().insert("DictSql.insertDictionary",dictionary);
    }

    @Override
    public Integer deleteDictionaryById(String dictId) {
        Map<String,Object> params = new HashMap<>();
        params.put("dictId",dictId);
        return getSqlSession().delete("DictSql.deleteDictionaryById",params);
    }

    @Override
    public Integer deleteDictionaryByType(String dictType) {
        Map<String,Object> params = new HashMap<>();
        params.put("dictType",dictType);
        return getSqlSession().delete("DictSql.deleteDictionaryByType",params);
    }

    @Override
    public Integer deleteDictionaryByParent(String parentId) {
        Map<String,Object> params = new HashMap<>();
        params.put("parentId",parentId);
        return getSqlSession().delete("DictSql.deleteDictionaryByParent",params);
    }
}
